using System;
using System.ComponentModel;
using System.Windows.Forms;
using FlickrShots.Framework;

namespace FlickrShots.GUI.controls
{
	public class FlickrAuthenticationControl : UserControl
	{
		private Label label1;
        private Button completeAuthenticationButton;
        private Button AuthenticationButton;
        private Panel panel1;
        private Panel panel2;
        private Panel reauthenticatePanel;
        private Label label2;
        private Button clearAuthentication;

        private Container components = null;

		public FlickrAuthenticationControl()
		{
			InitializeComponent();
		}

		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FlickrAuthenticationControl));
            this.completeAuthenticationButton = new System.Windows.Forms.Button();
            this.AuthenticationButton = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.panel1 = new System.Windows.Forms.Panel();
            this.panel2 = new System.Windows.Forms.Panel();
            this.reauthenticatePanel = new System.Windows.Forms.Panel();
            this.label2 = new System.Windows.Forms.Label();
            this.clearAuthentication = new System.Windows.Forms.Button();
            this.panel1.SuspendLayout();
            this.panel2.SuspendLayout();
            this.reauthenticatePanel.SuspendLayout();
            this.SuspendLayout();
            // 
            // completeAuthenticationButton
            // 
            this.completeAuthenticationButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
            this.completeAuthenticationButton.Enabled = false;
            this.completeAuthenticationButton.Location = new System.Drawing.Point(48, 40);
            this.completeAuthenticationButton.Name = "completeAuthenticationButton";
            this.completeAuthenticationButton.Size = new System.Drawing.Size(144, 23);
            this.completeAuthenticationButton.TabIndex = 15;
            this.completeAuthenticationButton.Text = "Complete Authentication";
            this.completeAuthenticationButton.Click += new System.EventHandler(this.clickCompleteAuthenticate);
            // 
            // AuthenticationButton
            // 
            this.AuthenticationButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
            this.AuthenticationButton.Location = new System.Drawing.Point(48, 8);
            this.AuthenticationButton.Name = "AuthenticationButton";
            this.AuthenticationButton.Size = new System.Drawing.Size(144, 23);
            this.AuthenticationButton.TabIndex = 14;
            this.AuthenticationButton.Text = "Authenticate";
            this.AuthenticationButton.Click += new System.EventHandler(this.clickAuthenticate);
            // 
            // label1
            // 
            this.label1.Dock = System.Windows.Forms.DockStyle.Top;
            this.label1.Location = new System.Drawing.Point(0, 0);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(248, 88);
            this.label1.TabIndex = 16;
            this.label1.Text = resources.GetString("label1.Text");
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.panel2);
            this.panel1.Controls.Add(this.label1);
            this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
            this.panel1.Location = new System.Drawing.Point(0, 0);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(248, 168);
            this.panel1.TabIndex = 17;
            // 
            // panel2
            // 
            this.panel2.Controls.Add(this.AuthenticationButton);
            this.panel2.Controls.Add(this.completeAuthenticationButton);
            this.panel2.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.panel2.Location = new System.Drawing.Point(0, 96);
            this.panel2.Name = "panel2";
            this.panel2.Size = new System.Drawing.Size(248, 72);
            this.panel2.TabIndex = 17;
            // 
            // reauthenticatePanel
            // 
            this.reauthenticatePanel.Controls.Add(this.label2);
            this.reauthenticatePanel.Controls.Add(this.clearAuthentication);
            this.reauthenticatePanel.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.reauthenticatePanel.Location = new System.Drawing.Point(0, 192);
            this.reauthenticatePanel.Name = "reauthenticatePanel";
            this.reauthenticatePanel.Size = new System.Drawing.Size(248, 104);
            this.reauthenticatePanel.TabIndex = 18;
            this.reauthenticatePanel.Visible = false;
            // 
            // label2
            // 
            this.label2.Dock = System.Windows.Forms.DockStyle.Top;
            this.label2.Location = new System.Drawing.Point(0, 0);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(248, 48);
            this.label2.TabIndex = 1;
            this.label2.Text = "Click Reauthenticate to remove the Token from  the FlickrShots application";
            this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // clearAuthentication
            // 
            this.clearAuthentication.Location = new System.Drawing.Point(48, 64);
            this.clearAuthentication.Name = "clearAuthentication";
            this.clearAuthentication.Size = new System.Drawing.Size(144, 23);
            this.clearAuthentication.TabIndex = 0;
            this.clearAuthentication.Text = "Reauthenticate";
            this.clearAuthentication.Click += new System.EventHandler(this.clickClearAuthentication);
            // 
            // FlickrAuthenticationControl
            // 
            this.Controls.Add(this.reauthenticatePanel);
            this.Controls.Add(this.panel1);
            this.Name = "FlickrAuthenticationControl";
            this.Size = new System.Drawing.Size(248, 296);
            this.panel1.ResumeLayout(false);
            this.panel2.ResumeLayout(false);
            this.reauthenticatePanel.ResumeLayout(false);
            this.ResumeLayout(false);

        }
		#endregion

        private Authenticatr _authenticatr = new Authenticatr();
        private void clickAuthenticate(object sender, EventArgs e)
        {
            OnAuthenticationStarted();
        	_authenticatr.Authenticate();
            AuthenticationButton.Enabled = false;
            completeAuthenticationButton.Enabled = true;
            clearAuthentication.Enabled = false;
        }

        private void clickCompleteAuthenticate(object sender, EventArgs e)
        {
            _authenticatr.CompleteAuthentication();
            completeAuthenticationButton.Enabled = false;
            clearAuthentication.Enabled = true;
            reauthenticatePanel.Visible = true;
            OnAuthenticationCompleted();
        }
	    
	    private void clickClearAuthentication(object sender, EventArgs e)
	    {
	        _authenticatr.ClearAuthentication();
	        clearAuthentication.Enabled = false;
	        OnAuthenticationCleared();
	    }
	    
	    protected void OnAuthenticationCompleted()
	    {
	        if (AuthenticationCompleted != null)
	        {
                AuthenticationCompleted(this, EventArgs.Empty);
	        }
	    }
	    
	    protected void OnAuthenticationStarted()
	    {
	        if (AuthenticationStarted != null)
	        {
                AuthenticationStarted(this, EventArgs.Empty);
	        }
	    }
	    
        protected void OnAuthenticationCleared()
        {
            if (AuthenticationCleared != null)
            {
                AuthenticationCleared(this, EventArgs.Empty);
            }
        }
        public event EventHandler AuthenticationCompleted;
        public event EventHandler AuthenticationStarted;
        public event EventHandler AuthenticationCleared;

        internal void CompleteAuthentication()
        {
            clickCompleteAuthenticate(this, EventArgs.Empty);
        }
    }
}
